<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>文章列表--layui后台管理模板 2.0</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet"href="/ssm/static/system/layui/css/layui.css" media="all" />
	<link rel="stylesheet"href="/ssm/static/system/css/public.css" media="all" />
	<link rel="stylesheet"href="/ssm/static/common/js/zTree/css/zTreeStyle/zTreeStyle.css" media="all" />

<style type="text/css">
.site-doc-icon{margin-bottom: 0px; font-size: 0;}
.site-doc-icon li{display: inline-block; vertical-align: middle; width: 50px; line-height: 25px; padding: 10px 0; margin-right: -1px; margin-bottom: -1px; border: 1px solid #e2e2e2; font-size: 36px; text-align: center; color: #666; transition: all .3s; -webkit-transition: all .3s;}
.site-doc-icon li .layui-icon{display: inline-block; font-size: 36px;}

.site-doc-icon li .fontclass{display: none;}
.site-doc-icon li .name{color: #c2c2c2;}
.site-doc-icon li:hover{background-color: #f2f2f2; color: #000;}
</style>
</head>
<body class="childrenBody">

<form class="layui-form" style="wid70%;">
	<input id="cid" type="hidden" value=""/>
    <div class="layui-form-item">
        <label class="layui-form-label">分类名称：</label>
        <div class="layui-input-inline">
            <input id="cname" autocomplete="off" placeholder="请输入分类名称"  type="text" class="layui-input"  lay-verify="required" required />
        </div>
    </div>

	<div class="layui-form-item">
		<label class="layui-form-label">上级分类</label>
		<div class="layui-input-inline">
			<input type="text" id="pMenu" value="" autocomplete="off" class="layui-input" lay-verify="required" placeholder="请选择上级菜单">
			<input type="hidden" id="pid" value=""/>
			<input type="hidden" id="menu_path" value=""/>
			<input type="hidden" id="level" value=""/>
			<input type="hidden" id="parent_code" value=""/>
		</div>
	</div>

	<div class="layui-form-item layui-row layui-col-xs12">
		<div class="layui-input-inline">
			<button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="addMenu">  提 交  </button>
		</div>
	</div>
</form>



<script type="text/javascript" src="/ssm/static/system/layui/layui.js"></script>
<script type="text/javascript" src="/ssm/static/common/js/jquery/jquery-3.2.1.min.js" inline="none"></script>
<script type="text/javascript" src="/ssm/static/common/js/zTree/js/jquery.ztree.core.min.js" inline="none"></script>
<script type="text/javascript" inline="none">
layui.use(['form','table','element','laytpl','layer','tree'], function() {
    var layer = layui.layer;
	var form = layui.form;
	var table = layui.table;
    var laytpl = layui.laytpl;
    var $=layui.jquery;
	var router = layui.router();
	var keyId = getQueryVariable("cid");;
	loadInfo();
	function loadInfo(){
		if(keyId){
			$("#menuId").val(keyId);
			$.post('/ssm/classes/queryList.do?id='+keyId,function(res){
				data= res.body[0];
				 $("input[id=cid]").val(data.cid);
	            $("input[id=cname]").val(data.cname);
	            $("input[id=pid]").val(data.pid);
	            $("input[id=pMenu]").val(data.pname);
			})
		}
	}

	function getQueryVariable(variable){
		   var query = window.location.search.substring(1);
		   var vars = query.split("&");
		   for (var i=0;i<vars.length;i++) {
				   var pair = vars[i].split("=");
				   if(pair[0] == variable){return pair[1];}
		   }
		   return(false);
	}

	function showMenuTreeDialog(){
		layer.open({
			type: 1,
			title: '选择上级菜单',
			area: ['400px', '300px'],
			offset: 'auto',
			content: '<ul id="treeMenus" class="ztree" style="padding: 25px 80px;"></ul>',
			btn: ['确定','关闭'],
			btnAlign: 'c',
			yes: function(index){
				var treeObj = $.fn.zTree.getZTreeObj("treeMenus");
				var sNodes = treeObj.getSelectedNodes();
				var choseNodeName;
				var choseNodeId;
				var choseNodePath;
				var choseNodeLevel="0";
				var pNodeName;
		    	if (sNodes.length > 0) {
		    		choseNodeName=sNodes[0].name;
		    		choseNodeId=sNodes[0].id;

		    		var pathNodes=sNodes[0].getPath();

		    		if(pathNodes.length==1){//root
		    			choseNodePath="";
		    			choseNodeLevel="0";
		    			pNodeName="";
		    		}else if(pathNodes.length>1){
			    		for(var i=1;i<pathNodes.length;i++){
			    			if(i==1){
			    				choseNodePath=pathNodes[i].name;
			    				pNodeName=pathNodes[i].name;
			    			}else{
			    				choseNodePath=choseNodePath+"/"+pathNodes[i].name;
			    			}
			    			choseNodeLevel=i;
			    		}
		    		}

		    	}

				if(choseNodePath==""){
					$("#pMenu").val("根菜单");
				}else{
					$("#pMenu").val(choseNodePath);
				}

				$("#pid").val(choseNodeId);
				$("#menu_path").val(choseNodePath);
				$("#level").val(choseNodeLevel);
				$("#parent_code").val(pNodeName);
				layer.close(index);
			}
		});
		layer.load(1);
		var setting = {
			view: {
		        selectedMulti: false        //禁止多点选中
	        },
		    data: {
		    		key:{
		    			name:"name"
		    		},
		    		simpleData:{
					 	enable : true,//采用简单数据格式
						idKey : "id",//每个节点数据的id
						idPKey : "pId",
						rootPid : "0"//根节点的id
		    		}
		    }

		};
		$.post("/ssm/classes/allClassesTree.do",function(res) {
			var data = res.body;
			//console.log(data);
			$.fn.zTree.init($("#treeMenus"), setting, data);
			layer.closeAll('loading');
		},"json");
	}

    $('#pMenu').on('click', function() {

    		showMenuTreeDialog();

	});




    form.on("submit(addMenu)",function(data){
    	  var qUrl="/ssm/classes/save.do";
          $.post(qUrl,{
              cid : $("#cid").val(),
              cname : $("#cname").val(),
              pid : $("#pid").val(),
          },function(res){
           if(res.code=="01"){
               layer.closeAll();
               layer.msg("保存成功");
               //刷新父页面
               layui.table.reload('supplierTable', {});
           }else{
               layer.msg("保存失败");
           }
       });
          return false;
    })


    	//添加按钮点击事件
	$("#editMenuIconBtn").click(function(){
		showChooseIcon();
		return false;
	});


});

</script>
</body>
</html>